package com.yk.framework.db;

import java.util.UUID;

import com.yk.framework.entity.EntityPKCache;
import com.yk.framework.entity.ICoreBaseInfo;

/**
 * @author lifetime
 * 
 */
public class YKIDUtils {

	public static String generateID(String typeString) {
		if(typeString == null){
			return null;
		}
		return typeString + UUID.randomUUID().toString();
	}

	public static void main(String[] args) {
		System.out.println(generateID("yk_pk_user"));
	}

	public static ICoreBaseInfo generateInfoByID(String id) throws Exception {
		String typeString = getTypeStringByID(id);
		String entityClassName = EntityPKCache.getCache().getTypeMapEntity().get(typeString);
		Class ec = Class.forName(entityClassName);
		ICoreBaseInfo baseInfo = (ICoreBaseInfo) ec.newInstance();
		baseInfo.put("id", id);
		return baseInfo;
	}

	public static String getTable(String id) {
		String typeString = getTypeStringByID(id);
		return EntityPKCache.getCache().getTypeMapTable().get(typeString);
	}

	public static String getEntity(String id) {
		String typeString = getTypeStringByID(id);
		return EntityPKCache.getCache().getTypeMapEntity().get(typeString);
	}

	public static String getTypeStringByID(String id) {
		if (isValidLength(id)) {
			return id.substring(0, 10);
		}
		return null;
	}

	/**
	 * @param id
	 * @return
	 */
	public static boolean isValidLength(String id) {
		if (id != null && id.length() == 46) {
			return true;
		} else {
			throw new IllegalArgumentException(" >>>>>>>>>>>>>>>>>>>  id is valid !");
		}
	}
}
